package cn.itsource.zcn.product.service.impl;

import cn.itsource.zcn.pet.domain.Pet;
import cn.itsource.zcn.pet.query.PetQuery;
import cn.itsource.zcn.product.domain.Product;
import cn.itsource.zcn.product.domain.ProductDetail;
import cn.itsource.zcn.product.mapper.ProductDetailMapper;
import cn.itsource.zcn.product.mapper.ProductMapper;
import cn.itsource.zcn.product.query.ProductQuery;
import cn.itsource.zcn.product.service.IProductService;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * (Product)表服务实现类
 *
 * @author makejava
 * @since 2021-08-30 09:13:58
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper tProductMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    /**
     * 新增数据
     *
     * @param tProduct 实例对象
     */
    @Override
    @Transactional
    public void save(Product tProduct) {
        // 先保存product
        tProduct.setCreatetime(new Date());
        tProductMapper.save(tProduct);
        //在保存product——detail
        ProductDetail detail = tProduct.getDetail();
        detail.setProduct_id(tProduct.getId());
        productDetailMapper.save(detail);
    }

    /**
     * 修改数据
     *
     * @param tProduct 实例对象
     */
    @Override
    @Transactional
    public void update(Product tProduct) {
        //
        tProductMapper.update(tProduct);
        //取出细节表
        ProductDetail detail = tProduct.getDetail();
        //根据id取出
        ProductDetail productDetail = productDetailMapper.queryByProductId(tProduct.getId());
        productDetail.setIntro(detail.getIntro());
        productDetail.setOrderNotice(detail.getOrderNotice());
        productDetailMapper.update(productDetail);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    @Transactional
    public void delete(Long id) {
        tProductMapper.delete(id);
        productDetailMapper.deleteByProductId(id);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Product queryById(Long id) {
        return tProductMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<Product> queryAll() {
        return tProductMapper.queryAll();
    }

    //前台根据id查询
    @Override
    public Product queryByIdfront(Long id) {
        return tProductMapper.queryByIdfront(id);
    }

    //高级+分页查询
    @Override
    public QueryResult queryPage(ProductQuery productQuery, HttpServletRequest request) {
        Integer count = tProductMapper.count(productQuery);
        List<Product> data = tProductMapper.page(productQuery);
        return new QueryResult(data,count);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        tProductMapper.deleteBatch(ids);
    }

    @Override
    public void onsaleBatch(Long[] ids) {
        tProductMapper.onsaleBatch(ids);
    }

    @Override
    public void offsaleBatch(Long[] ids) {
        tProductMapper.offsaleBatch(ids);
    }

    @Override
    public QueryResult queryPage(ProductQuery productQuery) {
        Integer count = tProductMapper.countfront(productQuery);
        List<Product> page = tProductMapper.pagefront(productQuery);
        return new QueryResult(page,count);
    }

}
